package gw.com.android.ui.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;

/**
 * @author jett
 * @since 2017-12-06.
 */
public class CornerEdgeTextView extends android.support.v7.widget.AppCompatTextView {

    private int mWidth;
    private int mHeight;
    private RectF rectF = new RectF();
    private Paint edgePaint = new Paint();
    private int edge = 2;

    public CornerEdgeTextView(Context context) {
        super(context);
        init();
    }

    public CornerEdgeTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CornerEdgeTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        edgePaint.setAntiAlias(true);
        edgePaint.setStyle(Paint.Style.STROKE);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = getMeasuredWidth();
        mHeight = getMeasuredHeight();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (mWidth > 0 && mHeight > 0) {
            rectF.set(edge, edge, mWidth - edge, mHeight - edge);
            edgePaint.setStrokeWidth(edge);
            edgePaint.setColor(getCurrentTextColor());
            canvas.drawRoundRect(rectF, mHeight / 2, mHeight / 2, edgePaint);
        }
        super.onDraw(canvas);
    }

}
